package com.lovo.pis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.pis.constant.MessageCode;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.dto.SupplierBean;
import com.lovo.pis.service.ISupplierService;
import com.lovo.pis.utils.ExcelImport;
import com.lovo.pis.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    ISupplierService supplierService;
    //模糊分页查询所有
    @RequestMapping("/queryAll")
    public IPage<SupplierBean> queryByItem(@RequestBody PageBean<SupplierBean> pageInfo){
       return supplierService.queryByItem(pageInfo);
    }

    //查询所有名称
    @RequestMapping("/queryAllName")
    public List<SupplierBean> queryAllName(){
       return supplierService.queryAllName();
    }

    //id查询单个
    @RequestMapping("/query")
    public SupplierBean queryById(int id){
       return supplierService.queryById(id);
    }

    //id修改单个
    @RequestMapping("/change")
    public JsonResult  changeById(@RequestBody SupplierBean supplierBean){
        JsonResult jsonResult = new JsonResult<>();
        supplierService.changeById(supplierBean);
        return jsonResult;
    }

    //添加
    @RequestMapping("/add")
    public JsonResult  addSupplier(@RequestBody SupplierBean supplierBean){
        JsonResult jsonResult = new JsonResult<>();
        supplierService.addSupplier(supplierBean);
        return jsonResult;
    }

    /**
     * Excel数据导入数据库
     * @return
     */
    @PostMapping("/dataImport")
    public String saveExcelDataTo(MultipartFile file) throws Exception {
        List<Object> objects = ExcelImport.importExcel(file,SupplierBean.class);
        List<SupplierBean> list = (List)objects;
        boolean flag = supplierService.saveExcelDataTo(list);
        if(flag){
            return "success";
        }
        return "error";
    }



}
